#include "ResourceMapping.h"


ResourceMapping::ResourceMapping(string str)
	:Component(COMP_RESOURCE_MAPPING)
{
	Initial(str);
}


ResourceMapping::~ResourceMapping(void)
{
}

void ResourceMapping::AddedToObject()
{
	RequestMessage(MSG_GET_PATH,&ResourceMapping::GetPath);
}


void ResourceMapping::Initial(string fileName)
{
	DataStream ds(fileName);
	Chunk* mainChunk=ds.GetMainChunk();
	if(!mainChunk->HasSubChunk())
		EXCEPTION("")
	for(Chunk::ChunkIterator iter=mainChunk->GetSubChunkBegin();iter!=mainChunk->GetSubChunkEnd();iter++)
	{
		
		const char* const data=(*iter)->GetData();
		const char* p=data;
		size_t namelength=strlen(data);
		string name;
		name=p;
		p+=namelength+1;
		size_t pathlength=strlen(p);
		string path=p;
		p+=pathlength+1;
		m_mapping[name]=path;
	}
}

void ResourceMapping::GetPath(const Message& msg)
{
	string name;
	string* path;
	AuxUnGroupArgus(msg.p,name,path);
	*path=GetPath(name);
}

string ResourceMapping::GetPath(string name)
{
	string path;
	if(m_mapping.find(name)!=m_mapping.end())
	{
		path=m_mapping[name];
	}
	else
	{
		EXCEPTION(name)
	}
	return path;
}
